﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace SystemAnalizer.Models
{
    public class SystemModel
    {
        protected DiscreteMaker discretemaker;
        public int number_discretes {get;set;}
        protected double[,,] A_descretematrix;
        protected string[,] A_matrix_input;
        public double m_CenterOfApproximation{get;set;}
        protected int system_size;
        public double H_step {get;set;}

        public int System_size
        {
            get { return this.system_size; }
        }
        public double[,,] System_Discretes
        {
            get { return this.A_descretematrix; }
        }
        public SystemModel(int number_of_discretes,int systemsize)
        {
            this.number_discretes = number_of_discretes;
            this.system_size = systemsize;
        }
        public SystemModel(int systemsize)
        {
            this.system_size = systemsize;
        }

        public void initMatrixInput(string[,] gridvalues)
        {
            this.A_matrix_input = new string[this.system_size, this.system_size];
            for (int i = 0; i < this.system_size; i++)
                for (int j = 0; j < this.system_size; j++)
                    this.A_matrix_input[i, j] = gridvalues[i, j];
        }

        public static void VisualMatrixMaker(int matrixsize, Grid matrixgrid)
        {
            matrixgrid.Height = 22 * matrixsize;
            matrixgrid.Width = 60 * matrixsize;
            for (int i = 0; i < matrixsize; i++)
            {
                RowDefinition row = new RowDefinition();
                row.MinHeight = 22;
                matrixgrid.RowDefinitions.Add(row);
                for (int j = 0; j < matrixsize; j++)
                {
                    ColumnDefinition column = new ColumnDefinition();
                    column.MinWidth = 60;
                    matrixgrid.ColumnDefinitions.Add(column);
                    TextBox valuetxt = new TextBox();
                    valuetxt.TextChanged += (o, e) =>
                    {
                        valuetxt.ToolTip = valuetxt.Text;
                    };

                    Grid.SetRow(valuetxt, i);
                    Grid.SetColumn(valuetxt, j);
                    matrixgrid.Children.Add(valuetxt);
                }
            }
        }

        public void SystemInserValtoContainerAdder(StackPanel container)
        {
            Label system_zise = new Label();
         //   system_zise.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            system_zise.Content = string.Format("System Size={0}", this.system_size);
            container.Children.Add(system_zise);
            
            Label Cent_aprox = new Label();
        //    Cent_aprox.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            Cent_aprox.Content = string.Format("Center of approximation={0}", this.m_CenterOfApproximation);
            container.Children.Add(Cent_aprox);
            
            Label h_step = new Label();
        //    h_step.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            h_step.Content = string.Format("H step={0}", this.H_step);
            container.Children.Add(h_step);

            Label number_ds = new Label();
        //    number_ds.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            number_ds.Content = string.Format("Number of discretes={0}", this.number_discretes);
            container.Children.Add(number_ds);
        }

        public void DiscretofMatrixtoContainerAdder(StackPanel container)
        {
            for(int l =0;l<this.number_discretes;l++)
            {
                StackPanel pnl = new StackPanel();
                Label lbl = new Label();
                lbl.Content = string.Format("discrete number {0}, A({0})", l);
                pnl.Children.Add(lbl);

                Grid grd = new Grid();

                for (int i = 0; i < this.system_size; i++)
                {
                    RowDefinition row = new RowDefinition();
                    row.MinHeight = 22;
                    grd.RowDefinitions.Add(row);
                    for (int j = 0; j < this.system_size; j++)
                    {
                        ColumnDefinition column = new ColumnDefinition();
                        column.MinWidth = 60;
                        grd.ColumnDefinitions.Add(column);
                        Label valuelbl = new Label();
                        valuelbl.Content = this.A_descretematrix[l, i, j];
                        //valuetxt.TextChanged += (o, e) =>
                        //{
                        //    valuetxt.ToolTip = valuetxt.Text;
                        //};

                        Grid.SetRow(valuelbl, i);
                        Grid.SetColumn(valuelbl, j);
                        grd.Children.Add(valuelbl);
                    }
                }
                pnl.Children.Add(grd);
                container.Children.Add(pnl);
            }
        }

    }
}
